package com.third.device.mg.serialport.help.netty

import com.third.device.mg.serialport.listener.MGNettyConnectListener
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.bytes.ByteArrayDecoder
import io.netty.handler.codec.bytes.ByteArrayEncoder


/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/12/31
 * Description:客户端通道初始化 用来进行设置出站解码器和入站编码器
 */
class ClientChannelInitializer(private val ip: String, val port: Int) :
    ChannelInitializer<SocketChannel>() {
    val clientHandler by lazy { ReadHandler(ip, port) }
    override fun initChannel(ch: SocketChannel?) {
        ch?.pipeline()?.addLast(ByteArrayDecoder())?.addLast(ByteArrayEncoder())
            ?.addLast(clientHandler)
    }

    fun setListener(listener: MGNettyConnectListener) {
        clientHandler.listener = listener
    }
}